﻿function ResizeEditPopup() {
    var popup = document.getElementById("editPopup")
    if (popup.style.display != 'none') {
        var width = getViewPortWidth();
        width -= 200;
        width -= popup.style.width.replace("px", "");
        popup.style.left = width / 2 + 'px';
        popup.style.top = "50px";
    }
}

function EditUser(username) {
    var popup = document.getElementById("editPopup");
    popup.style.display = 'block';
    ResizeEditPopup();
    document.getElementById('divUserManagement').style.opacity = 0.5;

    var rows = document.getElementById('dgUsers').getElementsByTagName('td');

    var n = 0;
    for (var i = 0; i < rows.length; i++) {
        if (rows[i].getAttribute('userName') == username) {
            switch (n) {
                case 0: //username
                    if (rows[i].innerHTML != '&nbsp;') {
                        document.getElementById('txtUserName').value = rows[i].innerHTML;
                    }
                    break;
                case 1: //name
                    if (rows[i].innerHTML != '&nbsp;') {
                        document.getElementById('txtName').value = rows[i].innerHTML;
                    }
                    break;
                case 2: //email
                    if (rows[i].innerHTML != '&nbsp;') {
                        document.getElementById('txtEmail').value = rows[i].innerHTML;
                    }
                    break;
                case 3: //department
                    if (rows[i].innerHTML != '&nbsp;') {
                        document.getElementById('txtDepartment').value = rows[i].innerHTML;
                    }
                    break;
                case 4: //address
                    if (rows[i].innerHTML != '&nbsp;') {
                        document.getElementById('txtAddress').value = rows[i].innerHTML;
                    }
                    break;
                case 5: //contact no
                    if (rows[i].innerHTML != '&nbsp;') {
                        document.getElementById('txtPrimaryContactNumber').value = rows[i].innerHTML;
                    }
                    break;
                case 6: //position                                    
                    document.getElementById('ddlType').value = rows[i].getAttribute('usertype');

                    break;
            }
            n++;
        }
    }
}

function UpdateUserInfo() {
    username = document.getElementById('txtUserName').value;
    name = document.getElementById('txtName').value;
    email = document.getElementById('txtEmail').value;
    department = document.getElementById('txtDepartment').value;
    address = document.getElementById('txtAddress').value;
    contactno = document.getElementById('txtPrimaryContactNumber').value;
    usertype = document.getElementById('ddlType').value;

    errMsg = "";
    if (username == "") {
        errMsg += "User Name is Required<br/>";
    }
    if (name == "") {
        errMsg += "Name is Required<br/>";
    }
    if (email == "") {
        errMsg += "Email is Required<br/>";
    }

    document.getElementById('lblErrorMessage').innerHTML = errMsg;

    if (errMsg != "") {
        return;
    }


    var url = "Administration/ManageUsers.aspx";
    url = url + "?updateuser=1";
    url = url + "&username=" + URLEncode(username);
    url = url + "&email=" + URLEncode(email);
    url = url + "&name=" + URLEncode(name);
    url = url + "&address=" + URLEncode(address);
    url = url + "&department=" + URLEncode(department);
    url = url + "&contactno=" + URLEncode(contactno);
    url = url + "&usertype=" + URLEncode(usertype);
    url = url + "&sid=" + Math.random();

    response = SubmitUserManagementForm(url);

    if (response.indexOf("Success") > -1) {
        loadXMLDoc('Administration/ManageUsers.aspx');
        alert("User details Successfully Updated");
    } else {
        document.getElementById('lblErrorMessage').innerHTML = response;
    }


}

function SubmitUserManagementForm(url) {

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    response = xmlhttp.responseText;
    //alert(response);
    response = response.substring(response.indexOf("<response>"), response.indexOf("</response"));
    //alert(response);

    return response;
}

function CancelSave() {
    document.getElementById('editPopup').style.display = 'none';
    document.getElementById('divUserManagement').style.opacity = 1.0;
}

function DeleteUser(username) {

    var result = confirm("Are you sure you want to delete this user?");
    
    if (result == 1) {
        var url = "Administration/ManageUsers.aspx";
        url = url + "?deleteuser=1";
        url = url + "&username=" + URLEncode(username);
        url = url + "&sid=" + Math.random();

        //alert(url);

        response = SubmitUserManagementForm(url);

        if (response.indexOf("Success") > -1) {
            loadXMLDoc('Administration/ManageUsers.aspx');
            alert("User Successfully Deleted");
        } else {
            alert("There was a problem deleting the user");
        }
    }
}